﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.DataAccess.UnitTest
{
    public class TestTool
    {
        public static string GetMetaJson(string fileName)
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PresetData", "MetaJson");
            path = Path.Combine(path, $"{fileName}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static string GetMetaJson(string fileName, params string[] paths)
        {
            var list = new List<string> { AppDomain.CurrentDomain.BaseDirectory };
            list.AddRange(paths);
            var path = Path.Combine(list.ToArray());
            path = Path.Combine(path, $"{fileName}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static string GetDataJson(string fileName)
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PresetData", "DataJson");
            path = Path.Combine(path, $"{fileName}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static string GetDataJson(string fileName, params string[] paths)
        {
            var list = new List<string> { AppDomain.CurrentDomain.BaseDirectory };
            list.AddRange(paths);
            var path = Path.Combine(list.ToArray());
            path = Path.Combine(path, $"{fileName}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static T GetMetaData<T>(string fileName)
        {
            var json = GetMetaJson(fileName);
            T obj = JsonConvert.DeserializeObject<T>(json);
            return obj;
        }

        public static T GetMetaData<T>(string fileName, params string[] paths)
        {
            var json = GetMetaJson(fileName, paths);
            T obj = JsonConvert.DeserializeObject<T>(json);
            return obj;
        }

        /// <summary>
        /// 构件TRecord
        /// </summary>
        /// <param name="fileName"></param>
        public static TRecord ConstructTRecordBy(string fileName, params string[] paths)
        {
            var json = GetDataJson(fileName, paths);
            JObject obj = JObject.Parse(json);
            Dictionary<string, object> dict = new Dictionary<string, object>();
            foreach (var prop in obj.Properties())
            {
                dict[prop.Name] = prop.Value;
            }
            TRecord record = new TRecord();
            foreach (var key in dict.Keys)
            {
                record.SetValue(key, dict[key]);
            }
            return record;
        }
    }
}
